﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.DataAccess;
using System.Data;
using TimKen.DataEntity;
using Common;
using TimKen.BussLogic;
namespace TimKen.Web.Shop
{
    public partial class InsertShop :TimKen.App.WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentUserName != "")
            {
                if (Request["isType"] != null && Request["isType"] == "1")
                {
                    this.Tx1.Visible = false;
                    this.Div5.Visible = false;
                }
                else if (Request["isType"] != null && Request["isType"] == "2")
                {
                    this.Tx2.Visible = false;
                    this.Div5.Visible = true;
                }

            }
            else
            {
                Response.Redirect("../Index.aspx");
            }
        }
        ProductManager pm = new ProductManager();
        UserAccountManager uam = new UserAccountManager();
        protected void Img_But_Click(object sender, ImageClickEventArgs e)
        {
            ProductDAO dao = new ProductDAO();
            if (Request["isType"] != null && Request["isType"] == "1")
            {
                if (this.AllItems.Value != "")
                {
                 string List = this.AllItems.Value;
                 string Lists=List.Replace("\r\n", "|");
                 Lists = Lists.Replace("\t", ",");
                 string[] Items = Lists.Split('|');
                 foreach (string Item in Items)
                 {
                     string[] str = Item.Split(',');
                     if (str != null&&str.Length==3)
                     {
                      DateTime date = DateTime.Now;
                      DateTime.TryParse(str[2], out date);
                      DataTable pro= dao.getProByCode(str[0]);
                      if (pro != null && pro.Rows.Count > 0)
                      {
                          Project project = new Project();
                          project.Date = Convert.ToDateTime(date);
                          project.Min = Convert.ToInt32(pro.Rows[0]["MinMultiple"]);
                          project.MinQuantitative = Convert.ToInt32(pro.Rows[0]["MinNumber"]);
                          project.Netweight = float.Parse(pro.Rows[0]["Weight"].ToString());
                          project.ProjectId = str[0];
                          project.ProjectXh = pro.Rows[0]["ItemClass"].ToString();
                          project.Unitprice = Convert.ToDecimal(pro.Rows[0]["BasicPrice"].ToString());
                          project.TaxRate = float.Parse(pro.Rows[0]["TaxRate"].ToString());
                          int num = 1;
                          int.TryParse(str[1], out num);
                          bool isAuthorizationed = pm.GetProductAuthorization(Convert.ToInt32(project.ProjectId), uam.GetModel(CurrentUserName).CustomerID.Value);
                          ShopCartItem item = new ShopCartItem(project, num,isAuthorizationed);
                          this.ShopCart.Add(item);
                      }
                      else {
                          continue;
                      }
                     }
                 
                 }
                 ClientScript.RegisterStartupScript(this.GetType(), "tanyo", "<script>CloseBox();</script>", false);
                }
            }
            else if (Request["isType"] != null && Request["isType"] == "2") {
                try
                {
                    System.IO.Stream tem = this.fl_Shop.PostedFile.InputStream;
                    int FileLength = fl_Shop.PostedFile.ContentLength;
                    byte[] bfile = new byte[FileLength];
                    tem.Read(bfile, 0, FileLength);
                    System.IO.FileStream file = new System.IO.FileStream("Excle",System.IO.FileMode.Create);
                    file.Write(bfile, 0, FileLength);
                    DataTable dt = NPOIHSSFHelper.Import(file);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr["ItemCode"].ToString().IndexOf("产品编号") >= 0)
                            {
                                continue;
                            }
                            DataTable pro = dao.getProByCode(dr["ItemCode"].ToString());
                            if (pro != null && pro.Rows.Count > 0)
                            {
                                Project project = new Project();
                                DateTime date = DateTime.Now;
                                DateTime.TryParse(dr["RequestDate"].ToString(), out date);
                                project.Date = Convert.ToDateTime(date);
                                project.Min = Convert.ToInt32(pro.Rows[0]["MinMultiple"]);
                                project.MinQuantitative = Convert.ToInt32(pro.Rows[0]["MinNumber"]);
                                project.Netweight = float.Parse(pro.Rows[0]["Weight"].ToString());
                                project.ProjectId = dr["ItemCode"].ToString();
                                project.ProjectXh = pro.Rows[0]["ItemClass"].ToString();
                                project.Unitprice = Convert.ToDecimal(pro.Rows[0]["BasicPrice"].ToString());
                                project.TaxRate = float.Parse(pro.Rows[0]["TaxRate"].ToString());
                                int num = 1;
                                int.TryParse(dr["Quantity"].ToString(), out num);
                                UserAccountManager uam = new UserAccountManager();
                                ProductManager pm = new ProductManager();
                                bool isAuthorizationed = pm.GetProductAuthorization(Convert.ToInt32(project.ProjectId), uam.GetModel(CurrentUserName).CustomerID.Value);
                                ShopCartItem item = new ShopCartItem(project, num,isAuthorizationed);
                                this.ShopCart.Add(item);
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    else {
                        Alert("请检查Excel格式!");
                    }
                    ClientScript.RegisterStartupScript(this.GetType(), "tanyo", "<script>CloseBox();</script>", false);
                }
                catch (Exception e1)
                {
                    Alert("文件读取有误!");
                }
            }
        }
    }
}